import struct
from collections import OrderedDict

from ..base import BaseParser


class GetHistoryInstrumentBarsRange(BaseParser):

    def __init__(self, *args, **kwargs):
        super(GetHistoryInstrumentBarsRange, self).__init__(self, *args, **kwargs)
        self.seqid = 1

    def setParams(self, market, code, date, date2):
        pkg = bytearray.fromhex('01')
        pkg.extend(struct.pack('<B', self.seqid))

        self.seqid += 1

        pkg.extend(bytearray.fromhex('38 92 00 01 16 00 16 00 0D 24'))
        code = code.encode('utf-8')

        # x =struct.pack("<B9s",  market, code)
        pkg.extend(struct.pack('<B9s', market, code))

        pkg.extend(bytearray.fromhex('07 00'))
        pkg.extend(struct.pack('<LL', date, date2))

        # print(hexdump.hexdump(pkg))
        self.send_pkg = pkg

    @staticmethod
    def _parse_date(num):
        year = num // 2048 + 2004
        month = (num % 2048) // 100
        day = (num % 2048) % 100

        return year, month, day

    @staticmethod
    def _parse_time(num):
        return (num // 60), (num % 60)

    def parseResponse(self, body_buf):
        klines = []
        pos = 12

        # 算了，前面不解析了，没太大用
        # (market, code) = struct.unpack("<B9s", body_buf[0: 10]
        (ret_count,) = struct.unpack('H', body_buf[pos: pos + 2])
        pos += 2

        for i in range(ret_count):
            (d1, d2, open_price, high, low, close, position, trade, settlementprice) = struct.unpack('<HHffffIIf', body_buf[pos:pos + 32])
            pos = pos + 32

            year, month, day = self._parse_date(d1)
            hour, minute = self._parse_time(d2)

            kline = OrderedDict([
                ('datetime', '%d-%02d-%02d %02d:%02d' % (year, month, day, hour, minute)),
                ('year', year),
                ('month', month),
                ('day', day),
                ('hour', hour),
                ('minute', minute),
                ('open', open_price),
                ('high', high),
                ('low', low),
                ('close', close),
                ('position', position),
                ('trade', trade),
                ('settlementprice', settlementprice)

            ])

            klines.append(kline)

        return klines
